package org.hzero.exam.app.service.impl;


import org.hzero.exam.api.dto.OrdersDTO;
import org.hzero.exam.app.service.HodrSoHeaderService;
import org.hzero.exam.domain.entity.HodrCompany;
import org.hzero.exam.domain.entity.HodrCustomer;
import org.hzero.exam.domain.entity.HodrSoHeader;
import org.hzero.exam.domain.entity.HodrSoLine;
import org.hzero.exam.domain.repository.HodrCompanyRepository;
import org.hzero.exam.domain.repository.HodrCustomerRepository;
import org.hzero.exam.domain.repository.HodrSoHeaderRepository;
import org.hzero.exam.infra.mapper.HodrSoLineMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author 马旭阳
 * @date2019/8/6
 */
@Service
public class HodrSoHeaderServiceImpl implements HodrSoHeaderService {
    @Autowired
    private HodrSoHeaderRepository soHeaderRepository;
    @Autowired
    private HodrSoLineMapper soLineMapper;
    @Autowired
    private HodrCompanyRepository companyRepository;
    @Autowired
    private HodrCustomerRepository customerRepository;

    @Override
    public void updateByOrderStatus(String orderStatus) {

        soHeaderRepository.updateByOrderStatus(orderStatus);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insert(OrdersDTO ordersDTO) {
        HodrSoHeader soHeader = ordersDTO.getHodrSoHeader();
        soHeaderRepository.insert(soHeader);
        HodrSoHeader soHeader1 = soHeaderRepository.selectByOrdernumber(soHeader.getOrderNumber());
        for(HodrSoLine hodrSoLine:ordersDTO.getHodrSoLines()){
            hodrSoLine.setSoHeaderId(soHeader1.getSoHeaderId());
            soLineMapper.insert(hodrSoLine);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(OrdersDTO ordersDTO) {
        if(ordersDTO!=null) {
            soHeaderRepository.updateByPrimaryKey(ordersDTO.getHodrSoHeader());
            for (HodrSoLine hodrSoLine:ordersDTO.getHodrSoLines()) {
                if (ordersDTO.getHodrSoHeader().getSoHeaderId().equals(hodrSoLine.getSoHeaderId())) {
                    soLineMapper.updateByPrimaryKey(hodrSoLine);
                }
            }
        }
    }

    @Override
    public void updateOne(HodrSoHeader hodrSoHeader) throws Exception{
        HodrCompany company=companyRepository.selectByPrimaryKey(hodrSoHeader.getCompanyId());
        HodrCustomer customer = customerRepository.selectByPrimaryKey(hodrSoHeader.getCustomerId());
        if (company!=null) {
            if (customer != null) {
                soHeaderRepository.updateByPrimaryKey(hodrSoHeader);
            }
        }
        new Exception("不好意思，出错了");
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAll(HodrSoHeader hodrSoHeader) {
        if (hodrSoHeader!=null) {
            List<HodrSoLine> hodrSoLines = soLineMapper.selectBySoheaderID(hodrSoHeader.getSoHeaderId());
            if (hodrSoLines != null) {
                for (HodrSoLine hodrSoLine : hodrSoLines) {
                    soLineMapper.delete(hodrSoLine);
                }
                soHeaderRepository.delete(hodrSoHeader);
            }
            soHeaderRepository.delete(hodrSoHeader);
        }
    }

    @Override
    public void insertOne(HodrSoHeader soHeader) throws Exception{
        HodrSoHeader hodrSoHeader = soHeaderRepository.selectByOrdernumber(soHeader.getOrderNumber());
        HodrCompany company=companyRepository.selectByPrimaryKey(soHeader.getCompanyId());
        HodrCustomer customer = customerRepository.selectByPrimaryKey(soHeader.getCustomerId());
        if (hodrSoHeader==null) {
            if (company!=null) {
                if (customer!=null) {
                soHeaderRepository.insert(soHeader);
                }
            }
            new Exception("不好意思，出错了");
        }

    }
}
